<!--
 * @Author: your name
 * @Date: 2022-03-19 17:40:09
 * @LastEditTime: 2022-04-19 04:29:16
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \react-antd-admin\vue-admin-template\src\views\category\categoryList.vue
-->
<template>
  <div>
    <el-button type="primary"
               size='mini'
               @click="toAdd">新增</el-button>
    <div class="table">
      <el-table v-loading='loading'
                size='mini'
                :data="tableData"
                style="width: 100%">
        <el-table-column width="80px"
                         type="index"
                         :index='1'
                         label="序号">
        </el-table-column>
        <el-table-column prop="name"
                         label="栏目名称">
        </el-table-column>
        <el-table-column prop="parentId"
                         label="父栏目">
        </el-table-column>
        <el-table-column prop="description"
                         label="栏目描述">
        </el-table-column>
        <el-table-column prop="no"
                         label="排序号">
        </el-table-column>
        <el-table-column label="操作"
                         width="100">
          <template slot-scope="scope">
            <el-button type="text"
                       size="small"
                       @click="toEdit(scope.row)">编辑</el-button>
            <el-divider direction="vertical"></el-divider>
            <el-button style="color:red"
                       type="text"
                       size="small"
                       @click="toDelete(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination-div">
        <el-pagination size="mini"
                       :current-page="params.page"
                       :page-sizes="[2,5,10, 15, 20]"
                       :page-size="params.pageSize"
                       layout="total, sizes, prev, pager, next, jumper"
                       :total="total"
                       @size-change="handleSizeChange"
                       @current-change="handleCurrentChange" />
      </div>
    </div>
    <div>
      <el-dialog :title="title"
                 :visible.sync="visible"
                 @close="toClose">
        <el-form ref="ruleForm"
                 :model="form"
                 :rules="rules">
          <el-form-item prop="name"
                        label="栏目名称"
                        :label-width="formLabelWidth">
            <el-input v-model="form.name"
                      autocomplete="off" />
          </el-form-item>
          <el-form-item prop="parentId"
                        label="父栏目"
                        :label-width="formLabelWidth">
            <!-- <el-select v-model="form.parentId"
                       clearable
                       placeholder="请选择父栏目">
              <el-option v-for="item in categoeies"
                         :key="item.id"
                         :label="item.name"
                         :value="item.id" />
            </el-select> -->
            <el-cascader v-model="form.parentId"
                         clearable
                         placeholder="请选择父栏目"
                         :options="categories"
                         filterable
                         :show-all-levels="false"
                         @change="handleChange"
                         :props="{ expandTrigger: 'hover', value: 'id', label: 'name' ,checkStrictly:true}" />
          </el-form-item>
          <el-form-item prop="no"
                        label="排序号"
                        :label-width="formLabelWidth">
            <el-input v-model="form.no"
                      autocomplete="off" />
          </el-form-item>
          <el-form-item prop="description"
                        label="栏目描述"
                        :label-width="formLabelWidth">
            <el-input v-model="form.description"
                      type="textarea"
                      :rows="2"
                      autocomplete="off" />
          </el-form-item>
        </el-form>
        <div slot="footer"
             class="dialog-footer">
          <el-button @click="visible=false">取 消</el-button>
          <el-button type="primary"
                     @click="toSave">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getAllCategory, deleteById, updateById, findCategoryTree, create } from '@/api/category'
export default {
  data() {
    return {
      tableData: [],
      categories: [],
      props: { expandTrigger: 'hover', value: 'id', label: 'name', checkStrictly: true },
      loading: false,
      title: '',
      visible: false,
      formLabelWidth: '80px',
      form: {},
      rules: {
        name: [{
          required: true,
          message: '请输入栏目名称',
          trigger: 'blur'
        }],
      },
      textarea: '',
      params: {
        page: 1,
        pageSize: 10
      },
      total: 0
    };
  },
  created() {
    this.pageCategoryData();
    this.getAllCategoryData()
  },
  watch: {
    params: {
      handler() {
        this.pageCategoryData()
      },
      deep: true
    }
  },
  methods: {
    async pageCategoryData() {
      this.loading = true;
      const res = await getAllCategory({ ...this.params });
      this.tableData = res.list;
      this.total = res.total
      this.loading = false;
    },
    async getAllCategoryData() {
      const res = await findCategoryTree()
      this.foo(res.data)
      this.categories = res.data
    },
    toDelete(id) {
      this.$confirm('此操作将永久删除该信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const res = await deleteById({ id })
        this.$message({ type: 'success', message: res.message })
        this.pageCategoryData()
        this.getAllCategoryData()
      })
    },
    toClose() {
      this.$refs.ruleForm.resetFields()
    },
    toAdd() {
      this.form = {}
      this.title = '添加栏目信息'
      this.visible = true
    },
    toEdit(row) {
      this.form = { ...row }
      this.title = '修改栏目信息'
      this.visible = true
    },
    toSave() {
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          let res;
          if (this.title === '添加栏目信息') {
            res = await create(this.form)
          }
          if (this.title === '修改栏目信息') {
            res = await updateById(this.form)
          }
          this.$notify.success({
            title: '成功',
            message: res.message
          })
          this.pageCategoryData()
          this.getAllCategoryData()
          this.visible = false
        } else {
          return false
        }
      })
    },
    /**
     * @description: 分页每页条数发生变化的回调
     * @param {*} val  当前条数值
     * @return {*}
     */
    handleSizeChange(val) {
      this.params.pageSize = val
      this.params.page = 1
    },
    /**
     * @description: 页码发生变化的回调
     * @param {*} val  当前条数值
     * @return {*}
     */
    handleCurrentChange(val) {
      this.params.page = val
    },
    /**
     * @description:  递归去除权限中的空children
     * @param {*} categories
     * @return {*}
     */
    foo(categories) {
      for (const p of categories) {
        if (p.children && p.children.length === 0) {
          delete p.children
        } else {
          this.foo(p.children)
        }
      }
    },
    handleChange(value) {
      console.log(value)
      this.form.parentId = value[value.length - 1]
    },
  }
};
</script>

<style scoped lang="less">
</style>
